package elementos;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class Boundary implements Drawable{
    private int x,y,width,height;
    
    private ArrayList<GoalListener> l;
    
    private ArrayList<KillBrickListener> l2;
    
    public Boundary(int x, int y, int width, int height){
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
        l2= new ArrayList<KillBrickListener>();
        l= new ArrayList<GoalListener>();
    }
    
    public void addGoalListener(GoalListener o){
        l.add(o);
    }
    public void clearCollisionListeners(){
        l.clear();
    }
    public void addKillBricklListener(KillBrickListener o){
        l2.add(o);
    }
    public void clearKillBrickListeners(){
        l2.clear();
    }
    private void goal(Ball b){
        for(int i=0;i<l.size();i++){
            l.get(i).onGoal(b);
        }
    }
    
    public void render(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Color c= g2.getColor();
        //g2.setBackground(Color.white);
        g2.setColor(Color.BLACK);
        g2.drawRect(x, y, width, height);
        g2.setColor(c);
    }

    public void add(Drawable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remove(Drawable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Drawable getChild(int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Drawable collision(Drawable d) {
        if(d instanceof Ball){
            Ball b = (Ball) d;
            float x = b.getX() - b.getRadio();
            float xf = b.getX() + b.getRadio();
            float y = b.getY() - b.getRadio();
            float yf = b.getY() + b.getRadio();
            if (x <= this.x) {
                //b.invertX();
                goal(b);
                return b;
            }
            if (xf >= this.x + width) {
                //b.invertX();
                goal(b);
                return b;
            }
            if (y <= this.y) {
                b.invertY();
                return b;
            }
            if (yf >= this.y + height) {
                b.invertY();
                return b;
            }
            return null;
        }
        if(d instanceof Bar){
            Bar b=(Bar)d;
            //colision con borde de arriba
            if(b.getY()<=this.y){
                return b;
            }
            if(b.getY()+b.getHeight()>=this.y+this.height){
                return b;
            }
            return null;
        }
        if(d instanceof Brick){
            Brick b = (Brick) d;
            //ldo 1
            if(b.getX()<this.x){
                Kill(b);
                return b;
            }
            if(b.getX()+b.getWidth()>this.x+this.width){
                Kill(b);
                return b;
            }
        }
        return null;
    }
    
    public void Kill(Brick b){
        for(int i=0;i<l2.size();i++){
            l2.get(i).onKill(b);
        }
    }
    
}
